{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 在这里我们演示一下 下单/交易/结算的整个流程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们首先会建立一个账户类和一个回测类\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    " # 初始化一个account\n",
    "user= QA.QA_User(username='admin',password='940809x')\n",
    "portfolio= user.new_portfolio('order_example')\n",
    "Account=portfolio.new_account()\n",
    "\n",
    "# 初始化一个回测类\n",
    "B = QA.QA_BacktestBroker()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在第一天的时候,全仓买入 000001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 全仓买入'000001'\n",
    "\n",
    "Order=Account.send_order(code='000001',\n",
    "                        price=11,\n",
    "                        money=0.04*Account.cash_available,\n",
    "                        time='2018-05-09',\n",
    "                        towards=QA.ORDER_DIRECTION.BUY,\n",
    "                        order_model=QA.ORDER_MODEL.MARKET,\n",
    "                        amount_model=QA.AMOUNT_MODEL.BY_MONEY\n",
    "                        )\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ORDER的占用资金: 39609.9\n"
     ]
    }
   ],
   "source": [
    "\n",
    "## 打印order的占用资金\n",
    "print('ORDER的占用资金: {}'.format((Order.amount*Order.price)*(1+Account.commission_coeff)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账户剩余资金 :960000.0\n"
     ]
    }
   ],
   "source": [
    "# 账户剩余资金\n",
    "print('账户剩余资金 :{}'.format(Account.cash_available))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.init_hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold_available"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此时的账户cash并未减少,因为此过程为申报订单(已委托 未成交状态)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回测类接受订单 并返回订单(如果创建成功)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order realorder_id Order_Pbp1zk4y datetime:2018-05-09 09:30:00 code:000001 amount:3600 price:10.955 towards:1 btype:stock_cn order_id:Order_Pbp1zk4y account:Acc_pGZ3xeaV status:queued >\n"
     ]
    }
   ],
   "source": [
    "rec_mes=B.receive_order(QA.QA_Event(order=Order))\n",
    "print(rec_mes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#B.query_orders(rec_mes.account_cookie,rec_mes.realorder_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>order_time</th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_price</th>\n",
       "      <th>order_price</th>\n",
       "      <th>status</th>\n",
       "      <th>order_amount</th>\n",
       "      <th>trade_amount</th>\n",
       "      <th>cancel_amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acc_pGZ3xeaV</th>\n",
       "      <th>Order_Pbp1zk4y</th>\n",
       "      <td>2018-05-09 09:31:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>10.96</td>\n",
       "      <td>10.955</td>\n",
       "      <td>trade_success</td>\n",
       "      <td>3600</td>\n",
       "      <td>3600</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        order_time    code  name  towards  \\\n",
       "account_cookie realorder_id                                                 \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y  2018-05-09 09:31:00  000001  None        1   \n",
       "\n",
       "                               trade_price  order_price         status  \\\n",
       "account_cookie realorder_id                                              \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y        10.96       10.955  trade_success   \n",
       "\n",
       "                               order_amount  trade_amount  cancel_amount  \n",
       "account_cookie realorder_id                                               \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y          3600          3600              0  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.query_orders(Account.account_cookie)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#pd.DataFrame(list(B.deal_message.values()),columns=B.orderstatus_headers).set_index(['account_cookie','realorder_id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>trade_time</th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_amount</th>\n",
       "      <th>trade_price</th>\n",
       "      <th>trade_money</th>\n",
       "      <th>trade_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acc_pGZ3xeaV</th>\n",
       "      <th>Order_Pbp1zk4y</th>\n",
       "      <td>2018-05-09 00:00:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>3600</td>\n",
       "      <td>10.96</td>\n",
       "      <td>39447.8595</td>\n",
       "      <td>Trade_fHmrQ0OJ</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        trade_time    code  name  towards  \\\n",
       "account_cookie realorder_id                                                 \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y  2018-05-09 00:00:00  000001  None        1   \n",
       "\n",
       "                               trade_amount  trade_price  trade_money  \\\n",
       "account_cookie realorder_id                                             \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y          3600        10.96   39447.8595   \n",
       "\n",
       "                                     trade_id  \n",
       "account_cookie realorder_id                    \n",
       "Acc_pGZ3xeaV   Order_Pbp1zk4y  Trade_fHmrQ0OJ  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.query_orders(Account.account_cookie,'filled')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_mes=B.query_orders(Account.account_cookie,'filled')\n",
    "res=trade_mes.loc[Order.account_cookie,Order.realorder_id]\n",
    "Order.trade(res.trade_id,res.trade_price,res.trade_amount,res.trade_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Order.trade((trade_id, trade_price, trade_amount, trade_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "账户类接收到回测返回的回报信息,更新账户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "      <th>frozen</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-09 00:00:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>10.96</td>\n",
       "      <td>3600</td>\n",
       "      <td>960534.136</td>\n",
       "      <td>Order_Pbp1zk4y</td>\n",
       "      <td>Order_Pbp1zk4y</td>\n",
       "      <td>Trade_fHmrQ0OJ</td>\n",
       "      <td>Acc_pGZ3xeaV</td>\n",
       "      <td>9.864</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount        cash        order_id  \\\n",
       "0  2018-05-09 00:00:00  000001  10.96    3600  960534.136  Order_Pbp1zk4y   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission  tax message  \\\n",
       "0  Order_Pbp1zk4y  Trade_fHmrQ0OJ   Acc_pGZ3xeaV       9.864    0    None   \n",
       "\n",
       "   frozen  \n",
       "0       0  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.history_table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此时我们可以打印一下现在的状态(现在的状态可以理解为在交易时 买入一只000001股票,但当天尚未收盘)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账户的可用资金 960534.136\n"
     ]
    }
   ],
   "source": [
    "print('账户的可用资金 {}'.format(Account.cash_available))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account.init_hold.index_name='code'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([Account.hold_available,Account.init_hold])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "+Account.hold_available"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们注意到 当最初申报订单的时候,可用资金只有950.2999999999302元,而买入成功后,可用资金有3339.9289999998837元,原因是下单的时候模式是市价单模式(QA.ORDER_MODEL.MARKET),故实际成交金额为10.96元"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "买入以后 账户的持仓为90800股 000001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "买入后账户现金表被扩展"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000, 960534.136]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.cash"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为是t+1的A股市场,故此时可卖数量为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.sell_available"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 执行结算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "Account.settle()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结算后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000, 960534.136]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "960534.136"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    3600\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.hold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 执行卖出操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在的持仓为: 000001 90800股"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "holdnum=Account.sell_available.get('000001',0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3600"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "holdnum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "申报一个卖出单,把可卖全部卖出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "Order=Account.send_order(code='000001',\n",
    "                        price=11,\n",
    "                        amount=holdnum,\n",
    "                        time='2018-05-10',\n",
    "                        towards=QA.ORDER_DIRECTION.SELL,\n",
    "                        order_model=QA.ORDER_MODEL.MARKET,\n",
    "                        amount_model=QA.AMOUNT_MODEL.BY_AMOUNT\n",
    "                        )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order realorder_id Order_YGcx8UP5 datetime:2018-05-10 09:31:00 code:000001 amount:3600 price:11 towards:-1 btype:stock_cn order_id:Order_YGcx8UP5 account:Acc_pGZ3xeaV status:queued >"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "960534.136"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.cash_available  # 因为此时订单尚未申报成功 可用现金不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order realorder_id Order_YGcx8UP5 datetime:2018-05-10 09:30:00 code:000001 amount:3600 price:11.0 towards:-1 btype:stock_cn order_id:Order_YGcx8UP5 account:Acc_pGZ3xeaV status:queued >\n"
     ]
    }
   ],
   "source": [
    "rec_mes=B.receive_order(QA.QA_Event(order=Order))\n",
    "print(rec_mes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_mes=B.query_orders(Account.account_cookie,'filled')\n",
    "res=trade_mes.loc[Order.account_cookie,Order.realorder_id]\n",
    "Order.trade(res.trade_id,res.trade_price,res.trade_amount,res.trade_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000084.636"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.cash_available # 此时订单已成交 cash_available立刻结转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "      <th>message</th>\n",
       "      <th>frozen</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-09 00:00:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>10.96</td>\n",
       "      <td>3600</td>\n",
       "      <td>960534.136</td>\n",
       "      <td>Order_Pbp1zk4y</td>\n",
       "      <td>Order_Pbp1zk4y</td>\n",
       "      <td>Trade_fHmrQ0OJ</td>\n",
       "      <td>Acc_pGZ3xeaV</td>\n",
       "      <td>9.864</td>\n",
       "      <td>0.0</td>\n",
       "      <td>None</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-05-10 00:00:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>11.00</td>\n",
       "      <td>-3600</td>\n",
       "      <td>1000084.636</td>\n",
       "      <td>Order_YGcx8UP5</td>\n",
       "      <td>Order_YGcx8UP5</td>\n",
       "      <td>Trade_6wnhQXyi</td>\n",
       "      <td>Acc_pGZ3xeaV</td>\n",
       "      <td>9.900</td>\n",
       "      <td>39.6</td>\n",
       "      <td>None</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount         cash        order_id  \\\n",
       "0  2018-05-09 00:00:00  000001  10.96    3600   960534.136  Order_Pbp1zk4y   \n",
       "1  2018-05-10 00:00:00  000001  11.00   -3600  1000084.636  Order_YGcx8UP5   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission   tax message  \\\n",
       "0  Order_Pbp1zk4y  Trade_fHmrQ0OJ   Acc_pGZ3xeaV       9.864   0.0    None   \n",
       "1  Order_YGcx8UP5  Trade_6wnhQXyi   Acc_pGZ3xeaV       9.900  39.6    None   \n",
       "\n",
       "   frozen  \n",
       "0       0  \n",
       "1       0  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_OrderQueue >"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.orders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Order_Pbp1zk4y': < QA_Order realorder_id Order_Pbp1zk4y datetime:2018-05-09 09:30:00 code:000001 amount:3600 price:10.955 towards:1 btype:stock_cn order_id:Order_Pbp1zk4y account:Acc_pGZ3xeaV status:success_all >,\n",
       " 'Order_YGcx8UP5': < QA_Order realorder_id Order_YGcx8UP5 datetime:2018-05-10 09:30:00 code:000001 amount:3600 price:11.0 towards:-1 btype:stock_cn order_id:Order_YGcx8UP5 account:Acc_pGZ3xeaV status:success_all >}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Account.orders.order_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试T0账户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    " # 初始化一个account\n",
    "    \n",
    "AccountT0=portfolio.new_account(running_environment=QA.RUNNING_ENVIRONMENT.TZERO,init_hold={'000001':10000},init_cash=200000)\n",
    "\n",
    "# 初始化一个回测类\n",
    "B = QA.QA_BacktestBroker()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cash': 200000, 'hold': {'000001': 10000}}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.init_assets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.init_hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是一个有10000股000001持仓的T0账户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "Order=AccountT0.send_order(code='000001',\n",
    "                        price=11,\n",
    "                        amount=AccountT0.sell_available.get('000001',0),\n",
    "                        time='2018-05-10',\n",
    "                        towards=QA.ORDER_DIRECTION.SELL,\n",
    "                        order_model=QA.ORDER_MODEL.MARKET,\n",
    "                        amount_model=QA.AMOUNT_MODEL.BY_AMOUNT\n",
    "                        )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-05-10 09:31:00'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Order.datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order realorder_id Order_FVBb5N7K datetime:2018-05-10 09:30:00 code:000001 amount:10000 price:11.0 towards:-1 btype:stock_cn order_id:Order_FVBb5N7K account:Acc_m7awZbrp status:queued >\n"
     ]
    }
   ],
   "source": [
    "rec_mes=B.receive_order(QA.QA_Event(order=Order))\n",
    "print(rec_mes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order realorder_id Order_FVBb5N7K datetime:2018-05-10 09:30:00 code:000001 amount:10000 price:11.0 towards:-1 btype:stock_cn order_id:Order_FVBb5N7K account:Acc_m7awZbrp status:queued >"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rec_mes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>trade_time</th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_amount</th>\n",
       "      <th>trade_price</th>\n",
       "      <th>trade_money</th>\n",
       "      <th>trade_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_cookie</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acc_m7awZbrp</th>\n",
       "      <th>Order_FVBb5N7K</th>\n",
       "      <td>2018-05-10 00:00:00</td>\n",
       "      <td>000001</td>\n",
       "      <td>None</td>\n",
       "      <td>-1</td>\n",
       "      <td>10000</td>\n",
       "      <td>11.0</td>\n",
       "      <td>110137.5</td>\n",
       "      <td>Trade_NWdFv20n</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        trade_time    code  name  towards  \\\n",
       "account_cookie realorder_id                                                 \n",
       "Acc_m7awZbrp   Order_FVBb5N7K  2018-05-10 00:00:00  000001  None       -1   \n",
       "\n",
       "                               trade_amount  trade_price  trade_money  \\\n",
       "account_cookie realorder_id                                             \n",
       "Acc_m7awZbrp   Order_FVBb5N7K         10000         11.0     110137.5   \n",
       "\n",
       "                                     trade_id  \n",
       "account_cookie realorder_id                    \n",
       "Acc_m7awZbrp   Order_FVBb5N7K  Trade_NWdFv20n  "
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.query_orders(AccountT0.account_cookie,'filled')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_mes=B.query_orders(AccountT0.account_cookie,'filled')\n",
    "res=trade_mes.loc[Order.account_cookie,Order.realorder_id]\n",
    "Order.trade(res.trade_id,res.trade_price,res.trade_amount,res.trade_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001   -10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2018-05-10 00:00:00']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Order.trade_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001   -10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.buy_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001   -10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 2, 5, 19, 47, 36, 73732)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.running_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-05-10 00:00:00'"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-10000"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.buy_available.get('000001')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "r=AccountT0.close_positions_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-05-10'"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for Order in r:\n",
    "    #print(vars(Order))\n",
    "    rec_mes=B.receive_order(QA.QA_Event(order=Order))\n",
    "    trade_mes=B.query_orders(AccountT0.account_cookie,'filled')\n",
    "    res=trade_mes.loc[Order.account_cookie,Order.realorder_id]\n",
    "    Order.trade(res.trade_id,res.trade_price,res.trade_amount,res.trade_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[200000, 309862.5, 199734.975]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001   -20000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], Name: amount, dtype: float64)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "AccountT0.settle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>000001</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-05-10</th>\n",
       "      <th>Acc_m7awZbrp</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           000001\n",
       "date       account_cookie        \n",
       "2018-05-10 Acc_m7awZbrp         0"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.daily_hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>cash</th>\n",
       "      <th>datetime</th>\n",
       "      <th>account_cookie</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-05-10</th>\n",
       "      <th>Acc_m7awZbrp</th>\n",
       "      <td>2018-05-10</td>\n",
       "      <td>199734.975</td>\n",
       "      <td>2018-05-10 00:00:00</td>\n",
       "      <td>Acc_m7awZbrp</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                date        cash             datetime  \\\n",
       "date       account_cookie                                               \n",
       "2018-05-10 Acc_m7awZbrp   2018-05-10  199734.975  2018-05-10 00:00:00   \n",
       "\n",
       "                          account_cookie  \n",
       "date       account_cookie                 \n",
       "2018-05-10 Acc_m7awZbrp     Acc_m7awZbrp  "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.daily_cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], dtype: float64)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.hold_price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2018-05-11 09:30:00'"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    10000\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AccountT0.sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk_t0=QA.QA_Risk(AccountT0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cash': 200000, 'hold': {'000001': 10000}}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_t0.init_assets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date\n",
       "2018-05-10    265.025\n",
       "Name: 0, dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_t0.init_cash-risk_t0.assets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.001325124999999927"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_t0.assets.iloc[-1]/risk_t0.init_cash-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'total_buyandsell': -99.99,\n",
       " 'total_tax': -110.0,\n",
       " 'total_commission': -55.03,\n",
       " 'total_profit': -265.02}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "risk_t0.profit_construct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试期货账户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "rb_ds=QA.QA_fetch_future_min_adv('RBL8','2019-01-01','2019-02-01','15min')\n",
    "\n",
    "AccountFuture=portfolio.new_account(init_cash=1000000,allow_sellopen=True,allow_t0=True,account_cookie='future_test',market_type=QA.MARKET_TYPE.FUTURE_CN,frequence=QA.FREQUENCE.FIFTEEN_MIN)\n",
    "\n",
    "\n",
    "#Account.reset_assets(10000000)\n",
    "Broker=QA.QA_BacktestBroker()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "#rb_ds.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 全仓买入'000001'\n",
    "hq=next(rb_ds.panel_gen)\n",
    "order=AccountFuture.send_order(code='RBL8',\n",
    "                        price=hq.open,\n",
    "                        money=AccountFuture.cash_available,\n",
    "                        time=hq.datetime[0],\n",
    "                        towards=QA.ORDER_DIRECTION.BUY_OPEN,\n",
    "                        order_model=QA.ORDER_MODEL.MARKET,\n",
    "                        amount_model=QA.AMOUNT_MODEL.BY_MONEY\n",
    "                        )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order realorder_id Order_2CQtTA8q datetime:2019-01-02 09:15:00 code:RBL8 amount:200 price:3409.5 towards:2 btype:future_cn order_id:Order_2CQtTA8q account:future_test status:queued >"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Broker.receive_order(QA.QA_Event(order=order,market_data=hq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_mes = Broker.query_orders(AccountFuture.account_cookie, 'filled')\n",
    "res = trade_mes.loc[order.account_cookie, order.realorder_id]\n",
    "order.trade(res.trade_id, res.trade_price, res.trade_amount, res.trade_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account a1 save\n",
      "account a2 save\n",
      "QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE\n",
      "QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE\n",
      "account Acc_pGZ3xeaV save\n",
      "account Acc_m7awZbrp save\n",
      "account future_test save\n"
     ]
    }
   ],
   "source": [
    "AccountFuture.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
